package dataaccess;

import java.util.List;

import play.db.jpa.JPA;
import play.db.jpa.Transactional;

public class BaseDao {

	public static void save(Object entity) {
		JPA.em().persist(entity);
	}

	public static void update(Object entity) {
		JPA.em().persist(entity);
	}

        @play.db.jpa.Transactional
	public static <T> T getById(Class<T> clazz, Long id) {
		return JPA.em().find(clazz, id);
	}

	@SuppressWarnings("unchecked")
        @play.db.jpa.Transactional
	public static <T> List<T> getAll(Class<T> clazz) {
		return JPA.em().createQuery(String.format("FROM %s", clazz.getName())).getResultList();
	}

	public static <T> void delete(T entity) {
		JPA.em().remove(entity);
	}
}
